package org.liuyunfeng.web;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: TanJingyu
 * @create: 2022-05-20 10:03
 **/
@RestController
@RequestMapping("/erwei")
public class ErweiController {

    @GetMapping
    public void erwei(HttpServletResponse response) throws IOException, WriterException {
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("erwei/二维码图片_5月20日10时00分13秒.png");
//
//        byte[] bytes = new byte[4116];
//
//        IOUtils.readFully(resourceAsStream, bytes);
//
//        ServletOutputStream outputStream = response.getOutputStream();
//        outputStream.write(bytes);

        ServletOutputStream outputStream = response.getOutputStream();



        // 定义二维码的参数
        HashMap<EncodeHintType, Object> map = new HashMap<EncodeHintType, Object>();
        // 定义二维码字符集
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 纠错等级
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 边距
        map.put(EncodeHintType.MARGIN, 1);

        BitMatrix bitMatrix = new MultiFormatWriter().encode("https://weixin.qianlima.com/qlmYFB/index.html?openid=oFNc6s7wkFYj7x5id2ozOcl6Oqeg&debug=1#/user/joinMembership", BarcodeFormat.QR_CODE, 500, 500, map);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
    }






    /** 定义二维码图片的宽度 */
    private static final int WIDTH = 500;
    /** 定义二维码图片的高度 */
    private static final int HEIGHT = 500;

    /** 定义LOGO图片的宽度 */
    private static final int LOGO_WIDTH = 100;
    /** 定义LOGO图片的高度 */
    private static final int LOGO_HEIGHT = 100;

    @GetMapping("/g")
    public String g(HttpServletResponse response) throws WriterException, IOException {
//        String url = "https://weixin.qianlima.com/qlmYFB/index.html?openid=oFNc6s7wkFYj7x5id2ozOcl6Oqeg&debug=1#/user/joinMembership";
        String url = "https://weixin.qianlima.com/yfb_wechat9199/members/retain";

        /** 定义Map集合封装二维码配置信息 */
        Map<EncodeHintType, Object> hints = new HashMap<>();
        /** 设置二维码图片的内容编码 */
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        /** 设置二维码图片的上、下、左、右间隙 */
        hints.put(EncodeHintType.MARGIN, 1);
        /** 设置二维码的纠错级别 */
        hints.put(EncodeHintType.ERROR_CORRECTION,
                ErrorCorrectionLevel.H);
        /**
         * 创建二维码字节转换对象
         * 第一个参数：二维码图片中的内容
         * 第二个参数：二维码格式器
         * 第三个参数：生成二维码图片的宽度
         * 第四个参数：生成二维码图片的高度
         * 第五个参数：生成二维码需要配置信息
         *  */
        BitMatrix matrix = new
                MultiFormatWriter().encode(url,
                BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);

        /** 获取二维码图片真正的宽度  */
        int matrix_width = matrix.getWidth();
        /** 获取二维码图片真正的高度  */
        int matrix_height = matrix.getHeight();
        /** 定义一张空白的缓冲流图片 */
        BufferedImage image = new
                BufferedImage(matrix_width, matrix_height,
                BufferedImage.TYPE_INT_RGB);
        /** 把二维码字节转换对象 转化 到缓冲流图片上 */
        for (int x = 0; x < matrix_width; x++){
            for (int y = 0; y < matrix_height; y++){
                /** 通过x、y坐标获取一点的颜色 true: 黑色  false: 白色 */
//                int rgb = matrix.get(x, y) ? 0xFF1493 : 0xFFFFFF;
                int rgb = matrix.get(x, y) ? 0x000000 : 0xFFFFFF;
                image.setRGB(x, y, rgb);
            }
        }

        /** 获取公司logo图片 */
        BufferedImage logo = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("erwei/yfb_logo.jpg"));
        /** 获取缓冲流图片的画笔 */
        Graphics2D g = (Graphics2D)image.getGraphics();
        /** 在二维码图片中间绘制公司logo */
        g.drawImage(logo, (matrix_width - LOGO_WIDTH) / 2,
                (matrix_height - LOGO_HEIGHT) / 2,
                LOGO_WIDTH, LOGO_HEIGHT, null);

        /** 设置画笔的颜色 */
        g.setColor(Color.WHITE);
        /** 设置画笔的粗细 */
        g.setStroke(new BasicStroke(5.0f));
        /** 设置消除锯齿 */
        g.setRenderingHint(RenderingHints
                .KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        /** 绘制圆角矩形 */
        g.drawRoundRect((matrix_width - LOGO_WIDTH) / 2,
                (matrix_height - LOGO_HEIGHT) / 2,
                LOGO_WIDTH, LOGO_HEIGHT, 10, 10);

//        /** 向浏览器输出二维码 */
//        ImageIO.write(image, "png",
//                response.getOutputStream());

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8192);
        ImageIO.write(image, "png", byteArrayOutputStream);

        return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
    }


}

    